{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-23T05:10:17.478987Z",
     "start_time": "2022-06-23T05:10:10.345510Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import lightgbm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-23T04:39:59.926308Z",
     "start_time": "2022-06-23T04:39:59.642162Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1996</td>\n",
       "      <td>30.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>7.89</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1988</td>\n",
       "      <td>27.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>14.7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1988</td>\n",
       "      <td>36.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>85.0</td>\n",
       "      <td>7.131</td>\n",
       "      <td>0.00</td>\n",
       "      <td>40.1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1992</td>\n",
       "      <td>29.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>91.0</td>\n",
       "      <td>7.041</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>1998</td>\n",
       "      <td>42.0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.134</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6065</th>\n",
       "      <td>996</td>\n",
       "      <td>1</td>\n",
       "      <td>1990</td>\n",
       "      <td>50.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>87.0</td>\n",
       "      <td>5.125</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6066</th>\n",
       "      <td>997</td>\n",
       "      <td>0</td>\n",
       "      <td>1992</td>\n",
       "      <td>56.3</td>\n",
       "      <td>无记录</td>\n",
       "      <td>87.0</td>\n",
       "      <td>7.695</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6067</th>\n",
       "      <td>998</td>\n",
       "      <td>1</td>\n",
       "      <td>1992</td>\n",
       "      <td>23.8</td>\n",
       "      <td>无记录</td>\n",
       "      <td>85.0</td>\n",
       "      <td>3.194</td>\n",
       "      <td>7.50</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6068</th>\n",
       "      <td>999</td>\n",
       "      <td>0</td>\n",
       "      <td>2000</td>\n",
       "      <td>53.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>95.0</td>\n",
       "      <td>8.226</td>\n",
       "      <td>7.55</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6069</th>\n",
       "      <td>1000</td>\n",
       "      <td>1</td>\n",
       "      <td>1980</td>\n",
       "      <td>46.9</td>\n",
       "      <td>无记录</td>\n",
       "      <td>88.0</td>\n",
       "      <td>4.802</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6070 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        编号  性别  出生年份  体重指数          糖尿病家族史    舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  \\\n",
       "0        1   0  1996  30.1             无记录  106.0    3.818     7.89       0.0   \n",
       "1        2   0  1988  27.5             无记录   84.0   -1.000     0.00      14.7   \n",
       "2        3   1  1988  36.5             无记录   85.0    7.131     0.00      40.1   \n",
       "3        4   1  1992  29.5             无记录   91.0    7.041     0.00       0.0   \n",
       "4        5   0  1998  42.0  叔叔或者姑姑有一方患有糖尿病    NaN    7.134     0.00       0.0   \n",
       "...    ...  ..   ...   ...             ...    ...      ...      ...       ...   \n",
       "6065   996   1  1990  50.1             无记录   87.0    5.125     0.00       0.0   \n",
       "6066   997   0  1992  56.3             无记录   87.0    7.695     0.00       0.0   \n",
       "6067   998   1  1992  23.8             无记录   85.0    3.194     7.50       0.0   \n",
       "6068   999   0  2000  53.1             无记录   95.0    8.226     7.55       0.0   \n",
       "6069  1000   1  1980  46.9             无记录   88.0    4.802     0.00       0.0   \n",
       "\n",
       "      患有糖尿病标识  \n",
       "0           0  \n",
       "1           0  \n",
       "2           1  \n",
       "3           0  \n",
       "4           1  \n",
       "...       ...  \n",
       "6065       -1  \n",
       "6066       -1  \n",
       "6067       -1  \n",
       "6068       -1  \n",
       "6069       -1  \n",
       "\n",
       "[6070 rows x 10 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1=pd.read_csv('比赛训练集.csv',encoding='gbk')\n",
    "data2=pd.read_csv('比赛测试集.csv',encoding='gbk')\n",
    "#label标记为-1\n",
    "data2['患有糖尿病标识']=-1\n",
    "#训练集和测试机合并\n",
    "data=pd.concat([data1,data2],axis=0,ignore_index=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-23T04:58:41.414056Z",
     "start_time": "2022-06-23T04:58:41.375054Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th>BMI</th>\n",
       "      <th>DBP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "      <td>30.1</td>\n",
       "      <td>0</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>7.89</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>34</td>\n",
       "      <td>27.5</td>\n",
       "      <td>0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>14.7</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>34</td>\n",
       "      <td>36.5</td>\n",
       "      <td>0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>7.131</td>\n",
       "      <td>0.00</td>\n",
       "      <td>40.1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>29.5</td>\n",
       "      <td>0</td>\n",
       "      <td>91.0</td>\n",
       "      <td>7.041</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>24</td>\n",
       "      <td>42.0</td>\n",
       "      <td>1</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>7.134</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6065</th>\n",
       "      <td>996</td>\n",
       "      <td>1</td>\n",
       "      <td>32</td>\n",
       "      <td>50.1</td>\n",
       "      <td>0</td>\n",
       "      <td>87.0</td>\n",
       "      <td>5.125</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6066</th>\n",
       "      <td>997</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>56.3</td>\n",
       "      <td>0</td>\n",
       "      <td>87.0</td>\n",
       "      <td>7.695</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6067</th>\n",
       "      <td>998</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>23.8</td>\n",
       "      <td>0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>3.194</td>\n",
       "      <td>7.50</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6068</th>\n",
       "      <td>999</td>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>53.1</td>\n",
       "      <td>0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>8.226</td>\n",
       "      <td>7.55</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6069</th>\n",
       "      <td>1000</td>\n",
       "      <td>1</td>\n",
       "      <td>42</td>\n",
       "      <td>46.9</td>\n",
       "      <td>0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>4.802</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6070 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        编号  性别  出生年份  体重指数  糖尿病家族史    舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  \\\n",
       "0        1   0    26  30.1       0  106.0    3.818     7.89       0.0   \n",
       "1        2   0    34  27.5       0   84.0   -1.000     0.00      14.7   \n",
       "2        3   1    34  36.5       0   85.0    7.131     0.00      40.1   \n",
       "3        4   1    30  29.5       0   91.0    7.041     0.00       0.0   \n",
       "4        5   0    24  42.0       1   -1.0    7.134     0.00       0.0   \n",
       "...    ...  ..   ...   ...     ...    ...      ...      ...       ...   \n",
       "6065   996   1    32  50.1       0   87.0    5.125     0.00       0.0   \n",
       "6066   997   0    30  56.3       0   87.0    7.695     0.00       0.0   \n",
       "6067   998   1    30  23.8       0   85.0    3.194     7.50       0.0   \n",
       "6068   999   0    22  53.1       0   95.0    8.226     7.55       0.0   \n",
       "6069  1000   1    42  46.9       0   88.0    4.802     0.00       0.0   \n",
       "\n",
       "      患有糖尿病标识  BMI  DBP  \n",
       "0           0    3    2  \n",
       "1           0    3    1  \n",
       "2           1    4    1  \n",
       "3           0    3    2  \n",
       "4           1    4    0  \n",
       "...       ...  ...  ...  \n",
       "6065       -1    4    1  \n",
       "6066       -1    4    1  \n",
       "6067       -1    1    1  \n",
       "6068       -1    4    2  \n",
       "6069       -1    4    1  \n",
       "\n",
       "[6070 rows x 12 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "人体的成人体重指数正常值是在18.5-24之间\n",
    "低于18.5是体重指数过轻\n",
    "在24-27之间是体重超重\n",
    "27以上考虑是肥胖\n",
    "高于32了就是非常的肥胖。\n",
    "\"\"\"\n",
    "def BMI(a):\n",
    "    if a<18.5:\n",
    "        return 0\n",
    "    elif 18.5<=a<=24:\n",
    "        return 1\n",
    "    elif 24<a<=27:\n",
    "        return 2\n",
    "    elif 27<a<=32:\n",
    "        return 3\n",
    "    else:\n",
    "        return 4\n",
    "    \n",
    "data['BMI']=data['体重指数'].apply(BMI)\n",
    "data['出生年份']=2022-data['出生年份']  #换成年龄\n",
    "#糖尿病家族史\n",
    "\"\"\"\n",
    "无记录\n",
    "叔叔或者姑姑有一方患有糖尿病/叔叔或姑姑有一方患有糖尿病\n",
    "父母有一方患有糖尿病\n",
    "\"\"\"\n",
    "def FHOD(a):\n",
    "    if a=='无记录':\n",
    "        return 0\n",
    "    elif a=='叔叔或者姑姑有一方患有糖尿病' or a=='叔叔或姑姑有一方患有糖尿病':\n",
    "        return 1\n",
    "    else:\n",
    "        return 2\n",
    "    \n",
    "data['糖尿病家族史']=data['糖尿病家族史'].apply(FHOD)\n",
    "data['舒张压']=data['舒张压'].fillna(-1)\n",
    "\"\"\"\n",
    "舒张压范围为60-90\n",
    "\"\"\"\n",
    "def DBP(a):\n",
    "    if a<60:\n",
    "        return 0\n",
    "    elif 60<=a<=90:\n",
    "        return 1\n",
    "    elif a>90:\n",
    "        return 2\n",
    "    else:\n",
    "        return a\n",
    "data['DBP']=data['舒张压'].apply(DBP)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-23T05:35:00.747434Z",
     "start_time": "2022-06-23T05:35:00.723389Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>BMI</th>\n",
       "      <th>DBP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5070</th>\n",
       "      <td>0</td>\n",
       "      <td>35</td>\n",
       "      <td>33.1</td>\n",
       "      <td>0</td>\n",
       "      <td>72.0</td>\n",
       "      <td>6.586</td>\n",
       "      <td>24.16</td>\n",
       "      <td>2.94</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5071</th>\n",
       "      <td>0</td>\n",
       "      <td>24</td>\n",
       "      <td>20.6</td>\n",
       "      <td>1</td>\n",
       "      <td>68.0</td>\n",
       "      <td>3.861</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5072</th>\n",
       "      <td>1</td>\n",
       "      <td>43</td>\n",
       "      <td>42.1</td>\n",
       "      <td>0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>5.713</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.53</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5073</th>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>34.6</td>\n",
       "      <td>0</td>\n",
       "      <td>66.0</td>\n",
       "      <td>4.684</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.14</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5074</th>\n",
       "      <td>0</td>\n",
       "      <td>25</td>\n",
       "      <td>27.7</td>\n",
       "      <td>0</td>\n",
       "      <td>89.0</td>\n",
       "      <td>7.948</td>\n",
       "      <td>14.65</td>\n",
       "      <td>2.65</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6065</th>\n",
       "      <td>1</td>\n",
       "      <td>32</td>\n",
       "      <td>50.1</td>\n",
       "      <td>0</td>\n",
       "      <td>87.0</td>\n",
       "      <td>5.125</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6066</th>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>56.3</td>\n",
       "      <td>0</td>\n",
       "      <td>87.0</td>\n",
       "      <td>7.695</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6067</th>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>23.8</td>\n",
       "      <td>0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>3.194</td>\n",
       "      <td>7.50</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6068</th>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>53.1</td>\n",
       "      <td>0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>8.226</td>\n",
       "      <td>7.55</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6069</th>\n",
       "      <td>1</td>\n",
       "      <td>42</td>\n",
       "      <td>46.9</td>\n",
       "      <td>0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>4.802</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      性别  出生年份  体重指数  糖尿病家族史   舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  BMI  DBP\n",
       "5070   0    35  33.1       0  72.0    6.586    24.16      2.94    4    1\n",
       "5071   0    24  20.6       1  68.0    3.861     0.00      0.00    1    1\n",
       "5072   1    43  42.1       0  98.0    5.713     0.00      3.53    4    2\n",
       "5073   0    23  34.6       0  66.0    4.684     0.00      3.14    4    1\n",
       "5074   0    25  27.7       0  89.0    7.948    14.65      2.65    3    1\n",
       "...   ..   ...   ...     ...   ...      ...      ...       ...  ...  ...\n",
       "6065   1    32  50.1       0  87.0    5.125     0.00      0.00    4    1\n",
       "6066   0    30  56.3       0  87.0    7.695     0.00      0.00    4    1\n",
       "6067   1    30  23.8       0  85.0    3.194     7.50      0.00    1    1\n",
       "6068   0    22  53.1       0  95.0    8.226     7.55      0.00    4    2\n",
       "6069   1    42  46.9       0  88.0    4.802     0.00      0.00    4    1\n",
       "\n",
       "[1000 rows x 10 columns]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train=data[data['患有糖尿病标识'] !=-1]\n",
    "test=data[data['患有糖尿病标识'] ==-1]\n",
    "train_label=train['患有糖尿病标识']\n",
    "train=train.drop(['编号','患有糖尿病标识'],axis=1)\n",
    "test=test.drop(['编号','患有糖尿病标识'],axis=1)\n",
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-23T06:11:31.380315Z",
     "start_time": "2022-06-23T06:11:30.979316Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Info] Number of positive: 1936, number of negative: 3134\n",
      "[LightGBM] [Warning] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000303 seconds.\n",
      "You can set `force_row_wise=true` to remove the overhead.\n",
      "And if memory is not enough, you can set `force_col_wise=true`.\n",
      "[LightGBM] [Info] Total Bins 1063\n",
      "[LightGBM] [Info] Number of data points in the train set: 5070, number of used features: 10\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.381854 -> initscore=-0.481686\n",
      "[LightGBM] [Info] Start training from score -0.481686\n"
     ]
    }
   ],
   "source": [
    "def select_by_lgb(train_data,train_label,test_data,random_state=2022,metric='auc',num_round=300):\n",
    "    clf=lightgbm\n",
    "    train_matrix=clf.Dataset(train_data,label=train_label)\n",
    "\n",
    "    params={\n",
    "            'boosting_type': 'gbdt',  \n",
    "            'objective': 'binary',\n",
    "            'learning_rate': 0.1,\n",
    "            'metric': metric,\n",
    "            'seed': 2020,\n",
    "            'nthread':-1 }\n",
    "    model=clf.train(params,train_matrix,num_round)\n",
    "    pre_y=model.predict(test_data)\n",
    "    return pre_y\n",
    "\n",
    "test_data=select_by_lgb(train,train_label,test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-23T06:12:21.752273Z",
     "start_time": "2022-06-23T06:12:21.731273Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.041986</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000003</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.000328</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.002007</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.094745</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>0.000011</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>0.999985</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>0.000006</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>0.999976</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>0.000002</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            0  label\n",
       "0    0.041986      0\n",
       "1    0.000003      0\n",
       "2    0.000328      0\n",
       "3    0.002007      0\n",
       "4    0.094745      0\n",
       "..        ...    ...\n",
       "995  0.000011      0\n",
       "996  0.999985      1\n",
       "997  0.000006      0\n",
       "998  0.999976      1\n",
       "999  0.000002      0\n",
       "\n",
       "[1000 rows x 2 columns]"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pre_y=pd.DataFrame(test_data)\n",
    "pre_y['label']=pre_y[0].apply(lambda x:1 if x>0.5 else 0)\n",
    "pre_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-23T06:12:41.498948Z",
     "start_time": "2022-06-23T06:12:41.481992Z"
    }
   },
   "outputs": [],
   "source": [
    "result=pd.read_csv('提交示例.csv')\n",
    "result['label']=pre_y['label']\n",
    "result.to_csv('baseline.csv',index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
